<?php
    /**
     * 真题阅读管理
     * Created by PhpStorm.
     * User: Administrator
     * Date: 15-6-17
     * Time: 下午2:37
     */
    namespace app\modules\exercise\controllers;

    use yii;
    use app\libs\ApiControl;
    use app\modules\exercise\models\ExerciseList;
    use app\modules\cn\models\ExerciseTopic;
    use app\modules\cn\models\ExerciseLevel;
    use app\modules\cn\models\ExerciseType;
    use app\modules\cn\models\ExerciseCategory;
    use app\modules\exercise\models\ExerciseListDetail;

    class ReadController extends ApiControl {
        public $layout = 'exercise';
        public $enableCsrfValidation = false;

        /**
         * @Title  [阅读列表]
         * @Author Don
         * @Date   2021-05-11
         * @return [type]
         */
        public function actionIndex()
        {
            // 题型
            $option['type'] = ExerciseType::getType(462);
            // 难度
            $option['level'] = ExerciseLevel::getLevel(462);
            // 话题
            $option['topic'] = ExerciseTopic::getTopic(462);
            // var_dump($_GET);
            $cate_id = Yii::$app->request->get('cate_id', '');
            $type = Yii::$app->request->get('type', '');
            $level = Yii::$app->request->get('level', '');
            $topic = Yii::$app->request->get('topic', '');
            $page  = Yii::$app->request->get('page',1);

            $cate_model = new ExerciseCategory();
            $cate = $cate_model->getAllCates(462);
            
            $cate_str = implode(',', array_column($cate,'cate_id'));
            // f_d($cate_str);
            $where = '1=1';
            if ($type) {
                $where .= " and type=$type";
            }
            if ($level) {
                $where .= " and level=$level";
            }
            if ($topic) {
                $where .= " and topic=$topic";
            }
            if ($cate_str) {
                $where .= " and cate_id in($cate_str)";
            }
            if($cate_id){
                $where .= " and cate_id = $cate_id";
            }
            // f_d($where);
            $model = new ExerciseList();
            $data = $model->getData($where, 15, $page);
            // f_d($data);
            return $this->render('index',[
                'data' => $data,
                'option' => $option,
                'type' => $type,
                'level' => $level,
                'topic' => $topic,
                'cate'=>$cate,
                'cate_id'=>$cate_id
                // 'block' => $this->block
            ]);
        }

        /**
         * [actionUpdate 修改]
         * @Title  [title]
         * @Author Don
         * @Date   2021-05-12
         * @return [type]     [description]
         */
        public function actionUpdate()
        {
            // 题型
            $option['type'] = ExerciseType::getType(462);
            // 难度
            $option['level'] = ExerciseLevel::getLevel(462);
            // 话题
            $option['topic'] = ExerciseTopic::getTopic(462);
            $cate_id = Yii::$app->request->get('cate_id');

            if ($_POST) {
                // f_d($_POST);
                $id = Yii::$app->request->post('id');
                $name = Yii::$app->request->post('name');
                $title = Yii::$app->request->post('title');
                $title_image = Yii::$app->request->post('title_image');
                $image = Yii::$app->request->post('image');
                $viewCount = Yii::$app->request->post('viewCount');
                $description = Yii::$app->request->post('description');
                $cnName = Yii::$app->request->post('cnName');
                $answer = Yii::$app->request->post('answer');
                $numbering = Yii::$app->request->post('numbering');
                $article = Yii::$app->request->post('article');
                $type = Yii::$app->request->post('type');
                $topic = Yii::$app->request->post('topic');
                $level = Yii::$app->request->post('level');
                $sentenceNumber = Yii::$app->request->post('sentenceNumber');
                
                $model = ExerciseList::findOne($id);
                $model->name = $name;
                $model->title = $title;
                $model->title_image = $title_image;
                $model->image = $image;
                $model->viewCount = $viewCount;
                $model->description = $description;
                $model->cnName = $cnName;
                $model->answer = $answer;
                $model->numbering = $numbering;
                $model->article = $article;
                $model->type = $type;
                $model->topic = $topic;
                $model->level = $level;
                $model->sentenceNumber = $sentenceNumber;
                if ($model->save()) {
                    $this->redirect('/exercise/read/index?cate_id='.$cate_id);
                } else {
                    die('<script>alert("保存失败，请重试");history.go(-1);</script>');
                }
            } else {
                $id = Yii::$app->request->get('id');
                $data = ExerciseList::findOne($id);
                // f_d($data);
                return $this->render('update', [
                    'option' => $option,
                    'data' => $data,
                    'id' => $id,
                    'cate_id'=>$cate_id
                ]);
            }
        }

        /**
         * [actionDetail 详情]
         * @Title  [title]
         * @Author Don
         * @Date   2021-05-12
         * @return [type]     [description]
         */
        public function actionDetail()
        {
            $id = Yii::$app->request->get('id');
            $list = ExerciseList::findOne($id);
            $pid = $list->content_id;
            $data = ExerciseListDetail::find()->where(['pid'=>$pid])->asArray()->all();
            // f_d($data);
            return $this->render('detail', [
                'data' => $data,
                'id' => $id
            ]);
        }

        /**
         * [actionUpdateDetail 修改详情]
         * @Title  [title]
         * @Author Don
         * @Date   2021-05-12
         * @return [type]     [description]
         */
        public function actionUpdateDetail()
        {
            if ($_POST) {
                // f_d($_POST);
                $id = Yii::$app->request->post('id');
                $catName = Yii::$app->request->post('catName');
                $description = Yii::$app->request->post('description');
                $answer = Yii::$app->request->post('answer');
                $article = Yii::$app->request->post('article');
                $analysis = Yii::$app->request->post('analysis');

                $model = ExerciseListDetail::findOne($id);
                $model->catName = $catName;
                $model->description = $description;
                $model->answer = $answer;
                $model->article = $article;
                $model->analysis = $analysis;
                if ($model->save()) {
                    $this->redirect('/exercise/read/update-detail?id='.$id);
                } else {
                    die('<script>alert("保存失败，请重试");history.go(-1);</script>');
                }

            } else {
                // 子题目id
                $id = Yii::$app->request->get('id');
                // f_d($id);
                $data = ExerciseListDetail::findOne($id);
                // f_d($data);
                return $this->render('update_detail', [
                'data' => $data,
                'id' => $id
            ]);
            }
        }

        public function actionAdd()
        {
            if ($_POST) {
                $cate_id = Yii::$app->request->post('cate_id');
                $cateData = ExerciseCategory::find()->where("cate_id={$cate_id}")->asArray()->one();
                $name = Yii::$app->request->post('name');
                $title = Yii::$app->request->post('title');
                $title_image = Yii::$app->request->post('title_image');
                $image = Yii::$app->request->post('image');
                $viewCount = Yii::$app->request->post('viewCount');
                $description = Yii::$app->request->post('description');
                $cnName = Yii::$app->request->post('cnName');
                $answer = Yii::$app->request->post('answer');
                $numbering = Yii::$app->request->post('numbering');
                $article = Yii::$app->request->post('article');
                $type = Yii::$app->request->post('type');
                $topic = Yii::$app->request->post('topic');
                $level = Yii::$app->request->post('level');
                $sentenceNumber = Yii::$app->request->post('sentenceNumber');
                $test_site = Yii::$app->request->post('test_site');

                $model = new ExerciseList();
                $model->pid = 0;
                $model->cate_id = $cate_id;
                $model->catName = $cateData['name'];
                $model->name = $name;
                $model->title = $title;
                $model->title_image = $title_image;
                $model->image = $image;
                $model->viewCount = $viewCount;
                $model->description = $description;
                $model->cnName = $cnName;
                $model->answer = $answer;
                $model->numbering = $numbering;
                $model->article = $article;
                $model->type = $type;
                $model->topic = $topic;
                $model->level = $level;
                $model->sentenceNumber = $sentenceNumber;
                $model->createTime = date('Y-m-d H:i:s');
                $model->userId = Yii::$app->session->get('adminId');
                $content_id = ExerciseList::getMaxContentId();
                $model->content_id = $content_id;
                $model->sort = $content_id;
                $model->show = 1;
                $model->test_site = $test_site;
                if ($model->save()) {
                    $this->redirect('/exercise/read/index?cate_id='.$cate_id);
                } else {
                    die('<script>alert("保存失败，请重试");history.go(-1);</script>');
                }
            } else {
                // 题型
                $option['type'] = ExerciseType::getType(462);
                // 难度
                $option['level'] = ExerciseLevel::getLevel(462);
                // 话题
                $option['topic'] = ExerciseTopic::getTopic(462);
                //分类
                $model = new ExerciseCategory();
                $option['cate'] = $model->getAllCates(462);
                $cate_id = Yii::$app->request->get('cate_id');

                return $this->render('add', [
                    'option' => $option,
                    'cate_id'=>$cate_id
                ]);
            }
        }

        public function actionAddDetail()
        {
            if ($_POST) {
                $pid = Yii::$app->request->post('pid');
                $name = Yii::$app->request->post('name');
                $description = Yii::$app->request->post('description');
                $answer = Yii::$app->request->post('answer');
                $article = Yii::$app->request->post('article');
                $analysis = Yii::$app->request->post('analysis');
                $data = ExerciseList::findOne($pid);

                $model = new ExerciseListDetail();
                $model->cate_id = $data['cate_id'];
                $model->pid = $data['content_id'];
                $model->catName = $data['catName'];
                $model->name = $name;
                $model->description = $description;
                $model->answer = $answer;
                $model->article = $article;
                $model->createTime = date('Y-m-d H:i:s');
                $model->userId = Yii::$app->session->get('adminId');
                $content_id = ExerciseList::getMaxContentId();
                $model->content_id = $content_id;
                $model->sort = $content_id;
                $model->analysis = $analysis;
                $model->show = 1;
                if ($model->save()) {
                    $this->redirect('/exercise/read/detail?id='.$pid);
                } else {
                    die('<script>alert("保存失败，请重试");history.go(-1);</script>');
                }

            } else {
                $id = Yii::$app->request->get('id');
                $data = ExerciseList::findOne($id);
                $cate = ExerciseCategory::find()->where("cate_id={$data->cate_id}")->asArray()->one();
                return $this->render('add_detail', ['id'=>$id,'cate'=>$cate]);
            }
        }
}